package com.yaodian100.ec.memberevent;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.coupon.entity.Coupon;
import com.yaodian100.ec.coupon.service.CouponService;
import com.yaodian100.ec.memberevent.entity.MemberEvent;
import com.yaodian100.ec.memberevent.service.MemberEventService;

/**
 * 加入会员送购物金 or coupon
 * 
 * @author apple.chang
 * 
 */
public class MemberEventAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -944128406248911578L;

	private MemberEventService memberEventService;
	private DomainService<Menu> menuService;
	@Autowired
	private CouponService couponService;
	private SimplePager pager = null;
	private List<MemberEvent> meList;
	private Menu meOnTypeMenu;
	private Menu meTypeMenu;
	private Menu channelNameMenu;
	private String id;

	/******* 查询页面参数 ********/
	private String nameParam;
	private String startDateParam;
	private String endDateParam;
	private String minDate;
	private String meOnTypeParam;
	private String meTypeParam;

	/******* 编辑页面参数 ********/
	private String asname;
	private String startDate;
	private String endDate;
	private String eminDate;
	private String meType;
	private String startTime;
	private String endTime;
	private String amount;
	private String coupon_uuid;
	private String action_result;
	private String memo;
	private String after;
	private String days;

	/**
	 * 新增
	 * 
	 * @return
	 */
	public String add() {
		asname = "";
		eminDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", new Date());
		DateFormat dateFormat = new SimpleDateFormat(EcConstant.PATTERN_DATE);
		startDate = dateFormat.format(new Date());
		endDate = dateFormat.format(DateUtils.addDays(new Date(), 7));
		startTime = "00:00";
		endTime = "00:00";
		id = "";
		meType = "1";
		coupon_uuid = null;
		amount = "0";
		memo = null;
		after = "0";
		days = "0";

		return "add";
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String edit() {
		MemberEvent me = memberEventService.get(id);
		asname = me.getApplySource();
		Date tmp = new Date();
		if (me.getStartDate().getTime() < System.currentTimeMillis()) {
			tmp = me.getStartDate();
		}
		eminDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", tmp);
		DateFormat dateFormat = new SimpleDateFormat(EcConstant.PATTERN_DATE);
		startDate = dateFormat.format(me.getStartDate());
		endDate = dateFormat.format(me.getEndDate());
		dateFormat = new SimpleDateFormat("HH:mm");
		startTime = dateFormat.format(me.getStartDate());
		endTime = dateFormat.format(me.getEndDate());
		meType = me.getType();
		coupon_uuid = me.getCoupon_uuid();
		amount = me.getAmount().toString();
		after = Integer.toString(me.getWaitActivateDays());
		days = Integer.toString(me.getAvailableDays());
		memo = me.getMemo();
		return "edit";
	}

	public String getAction_result() {
		return action_result;
	}

	public String getAfter() {
		return after;
	}

	public String getAmount() {
		return amount;
	}

	public String getAsname() {
		return asname;
	}

	public Menu getChannelNameMenu() {
		return channelNameMenu;
	}

	public String getCoupon_uuid() {
		return coupon_uuid;
	}

	public String getDays() {
		return days;
	}

	public String getEminDate() {
		return eminDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public String getEndDateParam() {
		return endDateParam;
	}

	public String getEndTime() {
		return endTime;
	}

	public String getId() {
		return id;
	}

	public List<MemberEvent> getMeList() {
		return meList;
	}

	public MemberEventService getMemberEventService() {
		return memberEventService;
	}

	public String getMemo() {
		return memo;
	}

	public Menu getMeOnTypeMenu() {
		return meOnTypeMenu;
	}

	public String getMeOnTypeParam() {
		return meOnTypeParam;
	}

	public String getMeType() {
		return meType;
	}

	public Menu getMeTypeMenu() {
		return meTypeMenu;
	}

	public String getMeTypeParam() {
		return meTypeParam;
	}

	public String getMinDate() {
		return minDate;
	}

	public String getNameParam() {
		return nameParam;
	}

	public SimplePager getPager() {
		return pager;
	}

	public String getStartDate() {
		return startDate;
	}

	public String getStartDateParam() {
		return startDateParam;
	}

	public String getStartTime() {
		return startTime;
	}

	public String loadCouponData() {
		Coupon coupon = couponService.get(coupon_uuid);
		if (null != coupon) {
			StringBuffer data = new StringBuffer();
			data.append("名称：").append(coupon.getName()).append("<br/>");
			data.append("叙述：").append(StringUtils.isNotBlank(coupon.getDescription()) ? coupon.getDescription() : "").append(
					"<br/>");
			data.append("面额：").append(coupon.getFaceValue()).append("<br/>");
			data.append("数量：").append(coupon.getAmount()).append("<br/>");
			action_result = data.toString();
		} else {
			action_result = "代码错误";
		}
		return "jsonCouponData";
	}

	public void prepare() throws Exception {
		if (null == pager) {
			pager = new SimplePager();
		}
		if (null == meOnTypeMenu) {
			meOnTypeMenu = menuService.get("MemberEvent.OnType");
		}
		if (null == meTypeMenu) {
			meTypeMenu = menuService.get("MemberEvent.Type");
		}
		if (null == channelNameMenu) {
			Menu tempMenu = menuService.get("Channel.Name");
			channelNameMenu = new Menu();
			channelNameMenu.addOption("000all", "所有渠道");

			Iterator<String> a = tempMenu.getOptions().keySet().iterator();
			while (a.hasNext()) {
				String s = a.next();
				channelNameMenu.addOption(s, tempMenu.getOptions().get(s).getName());
			}
		}
	}

	public String save() throws ParseException {
		MemberEvent me;
		if (StringUtils.isNotBlank(id)) {
			me = memberEventService.get(id);
		} else {
			me = new MemberEvent();
		}
		me.setApplySource(asname);
		DateFormat dateFormat = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP_HH_MM);
		Date sday = dateFormat.parse(startDate + " " + startTime);
		Date eday = dateFormat.parse(endDate + " " + endTime);
		me.setStartDate(sday);
		me.setEndDate(eday);
		me.setType(meType);
		me.setMemo(memo);
		if ("1".equals(me.getType())) {
			Coupon coupon = couponService.get(coupon_uuid);
			me.setCoupon_uuid(coupon_uuid);
			me.setAmount(coupon.getFaceValue());
			me.setWaitActivateDays(0);
			me.setAvailableDays(0);
		} else {
			me.setCoupon_uuid(null);
			me.setAmount(new BigDecimal(amount));
			me.setWaitActivateDays(Integer.parseInt(after));
			me.setAvailableDays(Integer.parseInt(days));
		}
		if (StringUtils.isNotBlank(id)) {
			if (null == memberEventService.update(me)) {
				return "edit";
			}
		} else {
			if (null == memberEventService.save(me)) {
				return "add";
			}
		}
		return "save_ok";
	}

	public String search() throws ParseException {
		minDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", DateUtil.getTodayAddDays(-60).getTime());
		// search value
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(nameParam)) {
			crit.addEq("applySource", nameParam);
		}
		DateFormat dateFormat = new SimpleDateFormat(EcConstant.PATTERN_DATE);
		if (StringUtils.isNotBlank(startDateParam)) {
			Date sday = dateFormat.parse(startDateParam);
			crit.addGe("startDate", sday);
		}
		if (StringUtils.isNotBlank(endDateParam)) {
			Date eday = dateFormat.parse(endDateParam);
			crit.addLe("endDate", eday);
		}
		if (StringUtils.isNotBlank(meOnTypeParam)) {
			Date now = new Date();
			if (meOnTypeParam.equals("WAIT")) {
				crit.addGe("startDate", now);
			} else if (meOnTypeParam.equals("ON")) {
				crit.addLe("startDate", now);
				crit.addGe("endDate", now);
			} else if (meOnTypeParam.equals("OVERDUE")) {
				crit.addLe("endDate", now);
			}
		}
		if (StringUtils.isNotBlank(meTypeParam)) {
			if (meTypeParam.equals("BONUS")) {
				crit.addEq("type", "2");
			} else if (meTypeParam.equals("COUPON")) {
				crit.addEq("type", "1");
			}
		}

		int contentSize = memberEventService.getListSize(crit).intValue();
		pager.setPageRecord(20);
		pager.setTotalSize(contentSize);
		meList = memberEventService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), crit,
				new String[] { "createDate desc" });
		return "list";
	}

	public void setAction_result(String actionResult) {
		action_result = actionResult;
	}

	public void setAfter(String after) {
		this.after = after;
	}

	public void setAmount(String amount) {
		this.amount = amount;
	}

	public void setAsname(String asname) {
		this.asname = asname;
	}

	public void setChannelNameMenu(Menu channelNameMenu) {
		this.channelNameMenu = channelNameMenu;
	}

	public void setCoupon_uuid(String couponUuid) {
		coupon_uuid = couponUuid;
	}

	public void setDays(String days) {
		this.days = days;
	}

	public void setEminDate(String eminDate) {
		this.eminDate = eminDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMeList(List<MemberEvent> meList) {
		this.meList = meList;
	}

	public void setMemberEventService(MemberEventService memberEventService) {
		this.memberEventService = memberEventService;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setMeOnTypeMenu(Menu meOnTypeMenu) {
		this.meOnTypeMenu = meOnTypeMenu;
	}

	public void setMeOnTypeParam(String meOnTypeParam) {
		this.meOnTypeParam = meOnTypeParam;
	}

	public void setMeType(String meType) {
		this.meType = meType;
	}

	public void setMeTypeMenu(Menu meTypeMenu) {
		this.meTypeMenu = meTypeMenu;
	}

	public void setMeTypeParam(String meTypeParam) {
		this.meTypeParam = meTypeParam;
	}

	public void setMinDate(String minDate) {
		this.minDate = minDate;
	}

	public void setNameParam(String nameParam) {
		this.nameParam = nameParam;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setStartDateParam(String startDateParam) {
		this.startDateParam = startDateParam;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

}
